home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 9- full screen mode / source / animalpane.java next >
Encoding:
Java Source  |  2000-06-23  |  15.2 KB  |  492 lines

  1. import java.awt.Dimension;
  2. import java.io.IOException;
  3. import java.io.FileNotFoundException;
  4. import java.util.Hashtable;
  5.  
  6. import quicktime.app.QTFactory;
  7. import quicktime.app.anim.Compositor;
  8. import quicktime.app.anim.TwoDSprite;
  9. import quicktime.app.anim.SWController;
  10. import quicktime.app.event.QTActionEvent;  
  11. import quicktime.app.event.QTActionListener;
  12. import quicktime.app.event.QTMouseTargetController; 
  13. import quicktime.app.image.GraphicsImporterDrawer; 
  14. import quicktime.app.image.ImagePresenter; 
  15. import quicktime.app.image.ImageDataSequence;
  16. import quicktime.app.image.ImageUtil;
  17. import quicktime.app.ui.ReleaseButton;
  18. import quicktime.app.ui.ButtonActivator;
  19. import quicktime.app.players.MoviePresenter;
  20. import quicktime.app.players.QTPlayer;
  21.  
  22. import quicktime.app.actions.GenericResponder;
  23. import quicktime.app.actions.MouseResponder;
  24.  
  25. import quicktime.io.QTFile;
  26. import quicktime.io.OpenMovieFile;
  27.  
  28. import quicktime.qd.QDRect;
  29. import quicktime.qd.QDGraphics;
  30. import quicktime.qd.QDColor; 
  31. import quicktime.qd.QDConstants; 
  32.  
  33. import quicktime.std.StdQTConstants;
  34. import quicktime.std.movies.Movie; 
  35. import quicktime.std.image.Matrix;
  36. import quicktime.std.image.GraphicsMode;
  37.  
  38. import quicktime.QTSession;
  39. import quicktime.QTException; 
  40.  
  41. /**
  42.  * QTZoo Module 8 - Using QuickTime Transitions
  43.  *
  44.  * @author Levi Brown
  45.  * @author Michael Hopkins
  46.  * @author Apple Computer, Inc.
  47.  * @version 8.0 4/10/2000
  48.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  49.  *    
  50.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  51.  *                ("Apple") in consideration of your agreement to the following terms, and your
  52.  *                use, installation, modification or redistribution of this Apple software
  53.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  54.  *                please do not use, install, modify or redistribute this Apple software.
  55.  *
  56.  *                In consideration of your agreement to abide by the following terms, and subject
  57.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  58.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  59.  *                reproduce, modify and redistribute the Apple Software, with or without
  60.  *                modifications, in source and/or binary forms; provided that if you redistribute
  61.  *                the Apple Software in its entirety and without modifications, you must retain
  62.  *                this notice and the following text and disclaimers in all such redistributions of
  63.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  64.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  65.  *                Apple Software without specific prior written permission from Apple.  Except as
  66.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  67.  *                are granted by Apple herein, including but not limited to any patent rights that
  68.  *                may be infringed by your derivative works or by other works in which the Apple
  69.  *                Software may be incorporated.
  70.  *
  71.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  72.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  73.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  74.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  75.  *                COMBINATION WITH YOUR PRODUCTS.
  76.  *
  77.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  78.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  79.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  80.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  81.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  82.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  83.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  84.  * 
  85.  * 
  86.  */
  87. public class AnimalPane extends ZooPane
  88. {
  89.     /**
  90.      *  Public default constructor
  91.      *  Creates the map button, and sets up the listeners
  92.      */
  93.     public AnimalPane()
  94.     {
  95.         QDRect size = new QDRect(MainFrame.WIDTH, MainFrame.HEIGHT);
  96.         try
  97.         {
  98.             loadSound("data/zebra/Zebra.au");
  99.  
  100.             QDGraphics gw = new QDGraphics (size);
  101.             compositor = new Compositor (gw, QDColor.white, 30, 1);
  102.             
  103.             displayImage( "data/zebra/ZebraBackground.jpg", 6, 5, 110 );        
  104.                         
  105.             addText( "data/zebra/Zebra.txt", 5, 15, 65, 415, 220 );
  106.             
  107.             addMovie( "data/zebra/Zebra.mov", 2, 425, 65 );
  108.             
  109.             ctr = new QTMouseTargetController( false );
  110.             ctr.addQTMouseListener( new PaneMouseListener( QDColor.lightGray, QDColor.white, QDColor.darkGray));
  111.             compositor.addController( ctr );            
  112.                 
  113.              TwoDSprite currentSprite;
  114.              areas = new Hashtable(1);
  115.             currentSprite = CreateRollover( "data/MapButton.jpg", 5, 12, 406 );
  116.             areas.put(currentSprite, "map");    
  117.             
  118.             compositor.getTimer().setRate(0);    
  119.         }
  120.         catch ( QTException qte )
  121.         {
  122.             qte.printStackTrace();
  123.         }
  124.     }    
  125.     
  126.     /**
  127.      * Listener object for the Movie controller buttons
  128.      * Called when the button is released
  129.      */
  130.     public class ButtonListener implements QTActionListener    
  131.     {
  132.         /**
  133.          * Called when the button is released
  134.          * @param ???
  135.          */
  136.         public void actionPerformed( QTActionEvent event ) 
  137.         {
  138.             System.out.println( "AnimalPane.actionPerformed() " );
  139.             try
  140.             {
  141.                 Object source = event.getSource();
  142.                 
  143.                 if (source.equals(playButton))            // Play >
  144.                 {
  145.                     if ( md.getRate() == 0 )
  146.                     {
  147.                         md.setRate( 1 );
  148.                     }
  149.                     stopButton.setCurrentImage( stopRel );
  150.                     stopButton.setReleasedImage( stopRel );
  151.                     playButton.setCurrentImage( playPlaying );
  152.                     playButton.setReleasedImage( playPlaying );
  153.                 }                
  154.                 else if (source.equals(stopButton))        // Stop []
  155.                 {
  156.                     if ( md.getRate() == 1 )
  157.                     {
  158.                         md.setRate( 0 );
  159.                     }
  160.                     stopButton.setCurrentImage( stopDeactive );
  161.                     stopButton.setReleasedImage( stopDeactive );
  162.                     playButton.setCurrentImage( playRel );
  163.                     playButton.setReleasedImage( playRel );
  164.                 }
  165.                 else if (source.equals(rewindButton))    // Rewind <<
  166.                 {
  167.                     md.setTime(0);
  168.                 }
  169.             }
  170.             catch (QTException exc )
  171.             {
  172.                 exc.printStackTrace();
  173.             }
  174.         }
  175.     }
  176.     
  177.     /**
  178.      * Loads a graphics file from a relative path and creates an ImagePresenter object
  179.      * @param ???
  180.      */
  181.     public ImagePresenter MakePresenterFromFile( String name )
  182.     {
  183.         try
  184.         {
  185.             QTFile file = new QTFile (QTFactory.findAbsolutePath (name));
  186.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  187.             return ImagePresenter.fromGraphicsImporterDrawer(drawer);
  188.         }
  189.         catch (IOException e)
  190.         {
  191.             e.printStackTrace();
  192.         }
  193.         catch (QTException e)
  194.         {
  195.             e.printStackTrace();
  196.         }
  197.         return null;    
  198.     }
  199.     
  200.     /**
  201.      * Called to do any setup after being set as the client of the QTCanvas.
  202.      * May be used to start effects running, movies playing, etc.
  203.      */
  204.     public void start()
  205.     {
  206.         buttonController.addQTMouseListener(buttonActivator);
  207.         try
  208.         {
  209.             stopButton.setCurrentImage(  stopRel );        // Stop button should not have focus
  210.             playButton.setCurrentImage(  playPlaying );  // Play button should get focus (blue arrow)
  211.             playButton.setReleasedImage( playPlaying );
  212.         }
  213.         catch( QTException e )
  214.         {
  215.             e.printStackTrace();
  216.         }
  217.         try
  218.         {
  219.             md.setRate(1);                                // start the player
  220.         }
  221.         catch( QTException e )
  222.         {
  223.             e.printStackTrace();
  224.         }
  225.         compositor.getTimer().setRate(1);                // start the compositor
  226.     }
  227.     
  228.     /**
  229.      * Called to do clean up after being removed as the client of the QTCanvas.
  230.      * Should be used to stop effects running, movies playing, etc.
  231.      */
  232.     public void stop()
  233.     {
  234.         System.out.println( "Stop" );
  235.         buttonController.removeQTMouseListener(buttonActivator);
  236.         compositor.getTimer().setRate(0);
  237.         try
  238.         {
  239.             md.setRate(0);
  240.         }
  241.         catch( QTException e )
  242.         {
  243.             e.printStackTrace();
  244.         }
  245.     }
  246.     
  247.     /**
  248.      *  Plays the sound file associated with the current AnimalPane
  249.      */
  250.     public void playSound()
  251.     {
  252.         if (player == null)
  253.             return;
  254.  
  255.         try
  256.         {
  257.             player.setTime(0);            //Start the sound at the beginning
  258.             player.startTasking();         //Make sure the player gets time to play the sound
  259.             player.setRate(1);             //Start playing
  260.         }
  261.         catch (QTException exc)
  262.         {
  263.             exc.printStackTrace();
  264.         }
  265.     }
  266.     
  267.     /**
  268.      * Creates a Movie from the specified path and adds it
  269.      * to the compositor in the hardcoded location.
  270.      * @param moviePath the relative path to the movie file to add.
  271.      * @param layer, the layer of the compositor to add the movie to.
  272.      * @param x the x coordinate location.
  273.      * @param y the y coordinate location.
  274.      * @see #makeMovie
  275.      */
  276.     protected void addMovie(String moviePath, int layer, int x, int y)
  277.     {
  278.         try
  279.         {
  280.             Movie m = makeMovie (new QTFile (QTFactory.findAbsolutePath (moviePath)));
  281.             md = new MoviePresenter (m);
  282.             md.setLocation (x, y);
  283.             // Add the Movie Controls
  284.             
  285.             playRel     = MakePresenterFromFile( "data/Play.jpg" );            // Play >
  286.             playPress   = MakePresenterFromFile( "data/PlayPressed.jpg" );
  287.             playPlaying = MakePresenterFromFile( "data/Playing.jpg" );
  288.             playButton = new MovieButton( playRel, playPress, playPlaying );
  289.             playButton.setLabel ("Play");
  290.             playButton.setLocation( x + 68, y + 152 );
  291.             
  292.             stopRel      = MakePresenterFromFile( "data/Stop.jpg" );        // Stop []
  293.             stopPress    = MakePresenterFromFile( "data/StopPressed.jpg" );
  294.             stopDeactive = MakePresenterFromFile( "data/Stopped.jpg" );
  295.             stopButton  = new MovieButton( stopRel, stopPress, stopDeactive );
  296.             stopButton.setLabel ("Stop");
  297.             stopButton.setLocation( x + 136, y + 152 );
  298.             
  299.             rewRel      = MakePresenterFromFile( "data/Rewind.jpg" );        // Rewind <<
  300.             rewPress    = MakePresenterFromFile( "data/RewindPressed.jpg" );
  301.             rewDeactive = MakePresenterFromFile( "data/Rewind.jpg" );
  302.             rewindButton = new MovieButton( rewRel, rewPress, rewDeactive );
  303.             rewindButton.setLabel( "Rewind" );
  304.             rewindButton.setLocation( x, y + 152 );
  305.             
  306.             ButtonListener bl = new ButtonListener();                        // create a new listener for the buttons
  307.             
  308.             playButton.addActionListener(bl);
  309.             stopButton.addActionListener(bl);
  310.             rewindButton.addActionListener(bl);
  311.             
  312.             compositor.addMember (md, layer);                                // add the movie presenter
  313.             compositor.addMember (playButton);                                //     and buttons to the compositor
  314.             compositor.addMember (stopButton);
  315.             compositor.addMember (rewindButton);
  316.             
  317.             buttonController = new QTMouseTargetController (false);    
  318.             buttonController.addMember(playButton);
  319.             buttonController.addMember(stopButton);
  320.             buttonController.addMember(rewindButton);
  321.         
  322.             compositor.addController (buttonController);
  323.             buttonActivator = new ButtonActivator();
  324.             
  325.             md.setRate(1);                                                    // start the movie        
  326.         }
  327.         catch (QTException exc)
  328.         {
  329.             exc.printStackTrace();
  330.         }
  331.         catch ( IOException exc )
  332.         {
  333.             exc.printStackTrace();
  334.         }    
  335.     }
  336.     
  337.     /**
  338.      * Utility routine loads an image from a file, renders it, and adds it to the compositor
  339.      * @param filename the name of the file to be displayed
  340.      * @param layer the layer in the compositor where the image will be displayed
  341.      * @param x the x coordinate of the top left corner where the image is to be displayed
  342.      * @param y the y coordinate of the top left corner where the image is to be displayed
  343.      */
  344.     protected void displayImage( String filename, int layer, int x, int y )
  345.     {
  346.         try
  347.         {    
  348.             QTFile file = new QTFile (QTFactory.findAbsolutePath (filename));
  349.             GraphicsImporterDrawer drawer = new GraphicsImporterDrawer(file);
  350.             ImagePresenter presenter = ImagePresenter.fromGraphicsImporterDrawer(drawer);
  351.             presenter.setLocation( x, y );
  352.             compositor.addMember(presenter, layer );
  353.         }
  354.         catch (QTException exc)
  355.         {
  356.             exc.printStackTrace();
  357.         }
  358.         catch (IOException exc)
  359.         {
  360.             exc.printStackTrace();
  361.         }        
  362.     }
  363.     
  364.     /**
  365.      * Reads a text file from the disk and displays it in the compositor
  366.      * @param textPath the relative path where the text is located
  367.      * @param layer, the layer of the compositor to add the text to.
  368.      * @param x the x coordinate location.
  369.      * @param y the y coordinate location.
  370.      * @param width the width of the area to display the text.
  371.      * @param height the height of the area to display the text.
  372.      */
  373.     protected void addText(String textPath, int layer, int x, int y, int width, int height)
  374.     {
  375.         try
  376.         {
  377.             TextPresenter text = new TextPresenter(textPath, new Dimension(width, height));
  378.             
  379.             ImagePresenter presenter = text.getPresenter();
  380.             Matrix theMatrix = new Matrix();
  381.             theMatrix.translate( (float) x, (float) y );
  382.             presenter.setMatrix(theMatrix);
  383.             compositor.addMember(presenter, layer);
  384.         }
  385.         catch ( QTException exc )
  386.         {
  387.             exc.printStackTrace();
  388.         }
  389.         catch ( FileNotFoundException exc )
  390.         {
  391.             exc.printStackTrace();
  392.         }    
  393.     }
  394.     
  395.     /**
  396.      * Loads the specified sound file from disk and prepares it for playing
  397.      * @param ???
  398.      */
  399.     protected void loadSound(String soundPath)
  400.     {
  401.         try
  402.         {
  403.             String soundLocation = QTFactory.findAbsolutePath(soundPath).getPath();
  404.             //this call works with a file://, http://, rtsp://located movie
  405.             player = (QTPlayer)QTFactory.makeDrawable ("file://" + soundLocation);
  406.         }
  407.         catch (IOException exc)
  408.         {
  409.             exc.printStackTrace();
  410.         }
  411.         catch (QTException exc)
  412.         {
  413.             exc.printStackTrace();
  414.         }
  415.     }
  416.     
  417.  
  418.     /**
  419.      * Opens the movie file and sets it up to be played.
  420.      * @param f a QTFile representing the movie to initialize.
  421.      * @return the movie, ready to play
  422.      * @see #addMovie
  423.      */
  424.     protected Movie makeMovie (QTFile f) throws IOException, QTException
  425.     {
  426.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  427.         Movie m = Movie.fromFile (movieFile);
  428.         m.getTimeBase().setFlags (StdQTConstants.loopTimeBase);    
  429.         return m;    
  430.     }
  431.     
  432.     /**
  433.      *    Creates a TwoDSprite that behaves as a simple rollover button (such as the map button)
  434.      */
  435.     protected TwoDSprite CreateRollover( String filename, int layer, int x, int y )
  436.     {
  437.         TwoDSprite sprite = null;
  438.         try
  439.         {
  440.             QTFile                 file = new QTFile( QTFactory.findAbsolutePath( filename ));
  441.             ImagePresenter         presenter = ImagePresenter.fromFile( file );
  442.             ImageDataSequence     dataSeq = new ImageDataSequence (presenter.getDescription());
  443.             
  444.             dataSeq.addMember( presenter.getImage() );
  445.             
  446.             if ((QTSession.isCurrentOS(QTSession.kWin32) && QTSession.getQTMajorVersion() == 3) == false)    //doesn't work on QT3.0.2 on Win
  447.                 dataSeq = ImageUtil.makeTransparent ( dataSeq, QDColor.white );        // recompress the image using white as the transparent color
  448.             
  449.             Matrix theMatrix = new Matrix();
  450.             theMatrix.translate( (float) x, (float) y );
  451.             sprite = new TwoDSprite( dataSeq, theMatrix, true, 10 );
  452.             
  453.             GraphicsMode normalGM = new GraphicsMode (QDConstants.blend, QDColor.lightGray);
  454.             sprite.setGraphicsMode (normalGM);
  455.             compositor.addMember(sprite, layer);
  456.             ctr.addMember( sprite );
  457.         }
  458.         catch ( QTException exc )
  459.         {
  460.             exc.printStackTrace();
  461.         }
  462.         catch ( IOException exc )
  463.         {
  464.             exc.printStackTrace();
  465.         }
  466.         
  467.         return sprite;    
  468.     }
  469.     
  470.     
  471.     protected QTPlayer player;
  472.     protected MoviePresenter md;
  473.     
  474.     protected ImagePresenter playRel;
  475.     protected ImagePresenter playPress;
  476.     protected ImagePresenter playPlaying;
  477.     protected MovieButton playButton;
  478.             
  479.     protected ImagePresenter stopRel;
  480.     protected ImagePresenter stopPress;
  481.     protected ImagePresenter stopDeactive;
  482.     protected MovieButton stopButton;
  483.     
  484.     protected ImagePresenter rewRel;
  485.     protected ImagePresenter rewPress;
  486.     protected ImagePresenter rewDeactive;
  487.     protected MovieButton rewindButton;
  488.     
  489.     protected QTMouseTargetController buttonController;
  490.     protected ButtonActivator buttonActivator;
  491. }
  492.